"""
常见的数据存储格式：json yaml properties xml csv
csv格式：在电子表格和数据库中常见的一种数据存储格式，以逗号隔开每个数据，对数据进行存储，存储到后缀为.csv的文件种
name,age,sex
tom,23,男
lucy,12,女

python库种有专门用来处理csv文件的库，import csv
csv.reader()
csv.DictReader()
csv.writer()
csv.DictWriter()
"""
import csv
# 读取文件
with open("score.csv","r",encoding="utf-8") as file:
    reader1 = csv.reader(file)
    # print(list(reader1))
    for s in reader1: # s是个列表，将每行的单元数据存储到该列表种
        if len(s)>0:
            print(f"{s[0]}-->{s[1]}-->{s[2]}")

with open("score.csv","r",encoding="utf-8") as file:
    reader1 = csv.DictReader(file)
    for r in reader1: # r代表一行数据，r是个字典，key：表头 value:单元数据
        print(f"{r['classid']}==>{r.get('name')}-->{r['score']}")

# with open("score.csv","a",encoding="utf-8") as file:
#     file.write("\n2,李四,89")

# with  open("score.csv","a",encoding="utf-8") as file:
#     writer1 = csv.writer(file)
#     writer1.writerow([3,"王五",89])
#     writer1.writerow([4,"王五",89])
#     writer1.writerows([[5,"王五",89],[6,"王五",89],[7,"王五",89]])

with open("score.csv","a",encoding="utf-8") as file:
    writer1 = csv.DictWriter(file,fieldnames=['classid','name','score'])
    writer1.writerow({"classid":4,"name":"赵六","score":78})
    writer1.writerows([{"classid":5,"name":"赵六","score":78},{"classid":6,"name":"赵六","score":78}])


